package com.cy.mall.service.dict.impl;

import com.cy.mall.comm.constant.CacheConstants;
import com.cy.mall.dao.mapper.dict.DictMapper;
import com.cy.mall.model.domain.dict.Dict;
import com.cy.mall.model.enums.DictType;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.dict.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 数据字典 service
 * @Author tanyilin
 * @Date 2019/4/18 16:06
 */
@Service
public class DictServiceImpl extends BaseService implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Override
    @Cacheable(value = CacheConstants.CACHE_VALUE_24H)
    public List<Dict> selectByType(DictType type) {
        return dictMapper.selectByType(type);
    }

    @Override
    @Cacheable(value = CacheConstants.CACHE_VALUE_24H)
    public Dict selectByTypeAndKey(DictType type, String key) {
        return dictMapper.selectByTypeAndKey(type, key);
    }

    @Override
    @Cacheable(value = CacheConstants.CACHE_VALUE_24H)
    public List<Long> selectProductIdsListInCache() {
        List<Long> productIdsList = new ArrayList<>();
        List<Dict> dictList = dictMapper.selectByType(DictType.MALL_GOODS_PRODUCT_ID);
        dictList.stream().forEach(d -> {
            productIdsList.add(Long.valueOf(d.getValue()));
        });
        return productIdsList;
    }
}
